package com.jmectf.jgn;

import java.io.IOException;
import java.net.URL;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

import com.captiveimagination.jgn.synchronization.SyncObjectManager;
import com.captiveimagination.jgn.synchronization.message.SynchronizeCreateMessage;
import com.captiveimagination.jgn.synchronization.message.SynchronizeRemoveMessage;
import com.jme.bounding.BoundingBox;
import com.jme.math.Vector3f;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.util.GameTaskQueueManager;
import com.jme.util.export.binary.BinaryImporter;
import com.jmectf.game.vehicle.Vehicle;

public class BattleSync implements SyncObjectManager {

	private Node scene;

	public void setScene(Node scene) {
		this.scene = scene;
	}

	public Object create(SynchronizeCreateMessage scm) {
		System.out.println("CREATING PLAYER!");
		return buildRemotePlayer();
	}

	public boolean remove(SynchronizeRemoveMessage srm, Object obj) {
		return removeRemotePlayer((Vehicle)obj);
	}

	private Vehicle buildRemotePlayer() {
		Future<Vehicle> future = GameTaskQueueManager.getManager().update(new Callable<Vehicle> () {
			public Vehicle call() throws Exception {
				Spatial model = null;
				try {
					URL path = getClass().getResource("/com/jmectf/data/tank.jme");
					BinaryImporter importer = new BinaryImporter();
					model = (Spatial)importer.load(path.openStream());
					model.setModelBound(new BoundingBox());
					model.updateModelBound();
					model.setLocalScale(2.0f);
					model.rotateUpTo(new Vector3f(0.0f, 0.0f, -1.0f));
					model.rotateUpTo(new Vector3f(.5f , 0.0f, 0.0f));
				} catch (IOException e) {
					System.out.println("Could not load model.");
					e.printStackTrace();
					System.exit(0);
				}
				Vehicle player = new Vehicle("Player Node", model);
				player.setAcceleration(15);
				player.setBraking(15);
				player.setTurnSpeed(2.5f);
				player.setWeight(25);
				player.setMaxSpeed(25);
				player.setMinSpeed(15);
				scene.attachChild(player);
				scene.updateGeometricState(0, true);
				player.setRenderQueueMode(Renderer.QUEUE_OPAQUE);
				scene.updateRenderState();
				return player;
			}
		});
		try {
			return future.get();
		} catch(Exception exc) {
			exc.printStackTrace();
		}
		return null;
	}

	private boolean removeRemotePlayer(Vehicle vehicle) {
		return vehicle.removeFromParent();
	}

}
